import CryptoJS from 'crypto-js'

//密钥(只能大小写字母+数字+长度最长32位)
// const key = CryptoJS.enc.Utf8.parse('QHilvk7D3Phq766hQHilvk7D3Phq766h');
//偏移量
// const iv = CryptoJS.enc.Utf8.parse('87456321558qwert');

export const encryptECB = (content, key) => {
    let skey = CryptoJS.enc.Utf8.parse(key);
    let srcs = CryptoJS.enc.Utf8.parse(content);
    let encrypted = CryptoJS.AES.encrypt(srcs, skey, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    }).toString();
    return encrypted;
};

export const decryptECB = (content, key) => {
    let skey = CryptoJS.enc.Utf8.parse(key);
    let decrypt = CryptoJS.AES.decrypt(content, skey, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    }).toString(CryptoJS.enc.Utf8)
    return decrypt;
};

export const encryptCBC = (content, key, iv) => {
    let skey = CryptoJS.enc.Utf8.parse(key);
    let siv = CryptoJS.enc.Utf8.parse(iv);
    let encrypted = CryptoJS.AES.encrypt(content, skey, {
        mode: CryptoJS.mode.CBC,// 加密模式
        padding: CryptoJS.pad.Pkcs7,// 填充方式
        iv: siv  // 偏移向量
    }).toString()
    return encrypted;
};

export const decryptCBC = (content, key, iv) => {
    let skey = CryptoJS.enc.Utf8.parse(key);
    let siv = CryptoJS.enc.Utf8.parse(iv);
    let decrypt = CryptoJS.AES.decrypt(content, skey, {
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7,
        iv: siv  // 偏移向量
    }).toString(CryptoJS.enc.Utf8)
    return decrypt;
};

export const encryptMD5 = (content) => {
    return CryptoJS.MD5(content).toString();
};




